package com.chaochis.wind.user.service.impl;

import com.chaochis.wind.common_exceptions.has_delete_exception.DataHasDeleteException;
import com.chaochis.wind.common_exceptions.has_delete_exception.UserHasTombstoneException;
import com.chaochis.wind.common_exceptions.not_active_exception.DataNotActiveException;
import com.chaochis.wind.common_exceptions.not_active_exception.UserNotActiveException;
import com.chaochis.wind.common_exceptions.not_exist_exception.DataNotExistException;
import com.chaochis.wind.common_exceptions.not_exist_exception.UserNotExistException;
import com.chaochis.wind.common_exceptions.other_login_exceptions.password_exception.UserPasswordErrorException;
import com.chaochis.wind.enums.AccountType;
import com.chaochis.wind.enums.DataStatus;
import com.chaochis.wind.user.service.WindAccountService;
import com.chaochis.wind.user.service.WindLoginService;
import com.chaochis.wind.user.service.WindUserService;
import com.chaochis.wind.user.vo.WindAccount;
import com.chaochis.wind.util.id_generator.IdGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

/**
 * 风旅用户后端登录功能实现
 */
@Service("windAdminLoginService")
public class WindAdminLoginServiceImpl implements WindLoginService {

  @Autowired
  @Qualifier("tokenGenerator")
  private IdGenerator tokenGenerator;

  @Autowired
  private WindAccountService windAccountService;

  @Autowired
  private WindUserService windUserService;

  /**
   * 执行得登录业务
   * @param windAccount 要登录得账号
   * @return 返回登录成功得token
   * @throws DataHasDeleteException 异常: 账号已被删除
   * @throws DataNotExistException 异常: 账号不存在
   * @throws DataNotActiveException 异常: 账号未激活
   * @throws UserPasswordErrorException 异常: 密码错误，登录失败
   */
  @Override
  public String login(WindAccount windAccount) throws DataHasDeleteException, DataNotExistException, DataNotActiveException, UserPasswordErrorException {
    if (AccountType.UserName.equals(windAccount.getAccountType())) {
      return this.loginByUserNameType(windAccount);
    } else {
      return "1";
    }
  }
  public String loginByUserNameType(WindAccount windAccount) throws DataHasDeleteException, DataNotExistException, DataNotActiveException, UserPasswordErrorException {
    var account = this.windAccountService.getWindAccountByAccountName(windAccount.getAccountName(), AccountType.UserName);
    // 如果找不到该用户，就抛出对应异常
    if (account == null) {
      throw new UserNotExistException("该用户不存在，请您检查一下是否输错了用户名？");
    }
    // 如果该用户状态不对，则也抛出异常
    if (DataStatus.Deleted.equals(account.getCusAccountStatus())) {
      throw new UserHasTombstoneException("该用户已被删除，请向联系管理员重新添加账号!");
    } else if (DataStatus.Inactivated.equals(account.getCusAccountStatus())) {
      throw new UserNotActiveException("该用户还未被激活，请联系管理员激活该账号!");
    }
    // 校验用户的密码
    var passwordIsCorrect = account.getAccountPassword().equals(windAccount.getAccountPassword());
    if (!passwordIsCorrect) {
      throw new UserPasswordErrorException("您输入的密码不正确，请重新输入密码!");
    }

    // 通过账号获取该账号绑定得用户
    var loginUser = this.windAccountService.getBoundWindUser(account.getAccountId());
    // 生成token并保存进redis中
    var token = this.tokenGenerator.generator();
    this.windUserService.saveTempUserInfoByToken(token, loginUser);
    return token;
  }
  @Override
  public Boolean logout() {
    return null;
  }
}
